package com.aha.firstchapter.fourth;

import java.io.IOException;
import java.util.Scanner;

/**
 * 去重排序
 * Created by wangxin on 2015/10/28.
 */
public class DuplicateRemovalAndSort {
    public static void main(String[] args) throws IOException {
        //BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //String str = br.readLine();
        Scanner scan = new Scanner(System.in);
        System.out.println("input sum:");
        int sum = scan.nextInt();
        int[] arr = new int[sum];
        for (int i = 0; i < sum; i++) {
            System.out.println("输入第" + (i + 1) + "个数：");
            int value = scan.nextInt();
            arr[i] = value;
        }
        DuplicateRemovalAndSort dra = new DuplicateRemovalAndSort();
        dra.bucketStart(arr);
        dra.bubbleStart(arr);
    }
    //桶排序实现
    public void bucketStart(int[] arr){
        //桶排序+去重
        int[] book = new int[1000];
        for (int i = 0; i < 1000; i++) {
            book[i] = 0;
        }
        for (int j = 0; j < arr.length; j++) {
            book[arr[j]]++;
        }
        for (int n = 0; n < 1000; n++) {
            if(book[n] > 0) System.out.print(book[n] + " ");
        }
    }
    //冒泡排序实现
    public void bubbleStart(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int t = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = t;
                }
            }
        }
        System.out.print("\n"+arr[0] + " ");//输出第一个数
        for (int n = 1; n < arr.length; n++) {
            if(arr[n]!=arr[n-1]) System.out.print(arr[n] + " ");
        }
    }
}
